﻿/**
 * MouseTracker
 * 
 * @author		Allen Chou
 * @version		1.0.0 (last update: Dec 7 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */


package idv.cjcat.display {
	import flash.display.DisplayObject;

	/**
	 * The <code>MouseTracker</code> class lets you track the current mouse position relative to a display object.
	 * 
	 * <p>
	 * A <code>MouseTracker</code> object acts as a simple object with properties <code>x</code> and <code>y</code>.
	 * Every time these properties are retrieved, the values obtained correspond to the current position of the mouse cursor.
	 * </p>
	 * 
	 * <p>
	 * A mouse tracker can be used with <code>DynaGraphics</code> drawing commands.
	 * Since upon every render call all the objects' <code>x</code> and <code>y</code> properties are retrieved,
	 * a mouse tracker behaves exactly as if its the mouse cursor.
	 * Therefore, you can use a mouse tracker in the drawing commands,
	 * creating the illusion that you are using the mouse cursor as a line's end point or a curve's control point.
	 * </p>
	 */
	public class MouseTracker {
		
		/**
		 * The display object whose coordinate is viewed as the origin.
		 */
		public var displayObject:DisplayObject;
		/**
		 * Whether the x value is locked.
		 */
		public var lockedX:Boolean;
		/**
		 * Whether the y value is locked.
		 */
		public var lockedY:Boolean;
		/**
		 * What value to lock the x value to.
		 */
		public var lockX:Number = 0;
		/**
		 * What value to lock the y value to.
		 */
		public var lockY:Number = 0;
		
		
		/**
		 * Creates a mouse tracker relative to a display object.
		 * @param	displayObject  The display object.
		 * @param	lockedX        Whether the x value is locked.
		 * @param	lockedY        Whether the y value is locked.
		 * @param	lockX          What value to lock the x value to.
		 * @param	lockY          What value to lock the y value to.
		 */
		public function MouseTracker(displayObject:DisplayObject, lockedX:Boolean = false, lockedY:Boolean = false, lockX:Number = 0, lockY:Number = 0) {
			this.displayObject = displayObject;
			this.lockedX = lockedX;
			this.lockedY = lockedY;
			this.lockX = lockX;
			this.lockY = lockY;
		}
		
		/**
		 * The current x coordinate of the mouse cursor relative to <code>displayObject</code>.
		 */
		public function get x():Number { return (lockedX)?(lockX):(displayObject.mouseX); }
		/**
		 * The current y coordinate of the mouse cursor relative to <code>displayObject</code>.
		 */
		public function get y():Number { return (lockedY)?(lockY):(displayObject.mouseY); }
		
		/**
		 * Returns a string representation of the mouse coordinate.
		 * @return The string.
		 */
		public function toString():String {
			return "(x = " + x + ", y = " + y + ")";
		}
		
	}
}